import * as THREE from 'three'
import * as TWEEN from '@tweenjs/tween.js';
/**
 * 
 * @param camera 相机
 * @param meshList 需要隐藏的模型列表
 * @param curMesh 需要聚焦的模型
 */
export function showSignRittal(camera:THREE.PerspectiveCamera,scene:THREE.Scene, meshList:any[], curMesh: THREE.Group) {
    let oldPosition = {
        x: camera.position.x,
        y: camera.position.y,
        z: camera.position.z
    }
    new TWEEN.Tween({
        // 用来更新相机位置
        x1: oldPosition.x, // 相机x
        y1: oldPosition.y, // 相机y
        z1: oldPosition.z, // 相机z
        // 用来更新模型位置
        x2: curMesh.position.x, 
        y2: curMesh.position.y,
        z2: curMesh.position.z,
        opacity: 0.5,
    }).to({
        x1: 0,
        y1: 100,
        z1: 300,
        x2: 0,
        y2: 0,
        z2: 0,
        opacity: -1
    }, 1000).easing(TWEEN.Easing.Cubic.InOut)
    .onUpdate(function (object) {
        camera.position.x = object.x1;
        camera.position.y = object.y1;
        camera.position.z = object.z1;
        curMesh.position.x = object.x2
        curMesh.position.y = object.y2
        curMesh.position.z = object.z2
        meshList.forEach(ele=>{
            let material = ele.material
            if(Array.isArray(material)) {
                material.forEach(m=>{
                    m.opacity = object.opacity
                })
            } else {
                material.opacity = object.opacity
            }
        })
    }).onComplete(()=>{
        meshList.forEach(ele=>{
            scene.remove(ele)
        })
    }).start();
}

/**关闭显示一个机柜的动画
 * 
 * @param camera 
 * @param scene 
 * @param meshList 
 * @param curMesh 
 */
export function closeSignRittal(camera:THREE.PerspectiveCamera,scene:THREE.Scene, meshList:any[], curMesh: THREE.Group){
    let oldPosition = {
        x: camera.position.x,
        y: camera.position.y,
        z: camera.position.z
    }
    scene.remove(curMesh)
    new TWEEN.Tween({
        // 用来更新相机位置
        x1: oldPosition.x, // 相机x
        y1: oldPosition.y, // 相机y
        z1: oldPosition.z, // 相机z
        opacity: 0,
    }).to({
        x1: 0,
        y1: 500,
        z1: 1000,
        opacity: 1,
    }, 1000).easing(TWEEN.Easing.Cubic.InOut)
    .onUpdate(function (object) {
        camera.position.x = object.x1;
        camera.position.y = object.y1;
        camera.position.z = object.z1;
        meshList.forEach(ele=>{
            ele.visible = true
            let material = ele.material
            if(Array.isArray(material)) {
                material.forEach(m=>{
                    m.opacity = object.opacity
                })
            } else {
                material.opacity = object.opacity
            }
        })
        
    }).onComplete(()=>{
        
    }).start();
}


/**开前门的动画效果
 * 
 * @param group 父模型
 * @param targe 点击的模型
 */
export function openFrontDoor(group:THREE.Group, targe:THREE.Mesh) {

    let geometry:any = targe.geometry
    let width = geometry.parameters.width
    let height = geometry.parameters.height
    let len = geometry.parameters.len

    let oldPosition = targe.position
    console.log(oldPosition)
    let oz = targe.position.z

    new TWEEN.Tween({
        rotationX: 0  
    }).to({
        rotationX: - Math.PI/2
    }, 1000).easing(TWEEN.Easing.Cubic.InOut)
    .onUpdate(function (object) {
        const angle = object.rotationX
        let x = toFixedFn(-1 * width/2 + Math.cos(angle) * width / 2)
        let z = toFixedFn(-1 * width/2 * Math.sin(angle))
        targe.position.set(x, oldPosition.y, z + oz)
        targe.rotation.y = angle
    }).onComplete(()=>{
        console.log("动画结束")
        console.log(targe)
        targe.userData.openStatus = true
        targe.userData.oldPositionZ = oz
    }).start();
}

export function closeFrontDoor(group:THREE.Group, targe:THREE.Mesh) {

    let geometry:any = targe.geometry
    let width = geometry.parameters.width

    let oldPosition = targe.position
    console.log(oldPosition)
    let oz = targe.userData.oldPositionZ||targe.position.z

    new TWEEN.Tween({
        rotationX: - Math.PI/2
    }).to({
        rotationX: 0
    }, 1000).easing(TWEEN.Easing.Cubic.InOut)
    .onUpdate(function (object) {
        const angle = object.rotationX
        let x = toFixedFn(-1 * width/2 + Math.cos(angle) * width / 2)
        let z = toFixedFn(-1 * width/2 * Math.sin(angle))
        targe.position.set(x, oldPosition.y, z + oz)
        targe.rotation.y = angle
    }).onComplete(()=>{
        console.log("动画结束")
        console.log(targe)
        targe.userData.openStatus = false
    }).start();
}

/**打开后门 */
export function openAfterDoor(group:THREE.Group, targe:THREE.Mesh) {
    // targe.position.set( 35, 0, -45-35)
    // targe.rotation.y = Math.PI/2
    // targe.userData.openStatus = true
    // return

    let geometry:any = targe.geometry
    let width = geometry.parameters.width
    let height = geometry.parameters.height
    let len = geometry.parameters.len

    let oldPosition = targe.position
    console.log(oldPosition)
    let oz = targe.position.z

    new TWEEN.Tween({
        rotationX: 0  
    }).to({
        rotationX: -Math.PI/2
    }, 1000).easing(TWEEN.Easing.Cubic.InOut)
    .onUpdate(function (object) {
        const angle = object.rotationX
        let x = toFixedFn( width/2 - Math.cos(angle) * width / 2)
        let z = toFixedFn( width/2 * Math.sin(angle) )
        targe.position.set(x, oldPosition.y, z + oz)
        targe.rotation.y = angle
    }).onComplete(()=>{
        console.log("动画结束")
        console.log(targe)
        targe.userData.openStatus = true
        targe.userData.oldPositionZ = oz
    }).start();
}
/**关闭后门 */
export function closeAfterDoor(group:THREE.Group, targe:THREE.Mesh) {
    // targe.position.set( 0, 0, -45)
    // targe.rotation.y = 0
    // targe.userData.openStatus = false
    // return

    let geometry:any = targe.geometry
    let width = geometry.parameters.width

    let oldPosition = targe.position
    console.log(oldPosition)
    let oz = targe.userData.oldPositionZ||targe.position.z

    new TWEEN.Tween({
        rotationX: - Math.PI/2
    }).to({
        rotationX: 0
    }, 1000).easing(TWEEN.Easing.Cubic.InOut)
    .onUpdate(function (object) {
        const angle = object.rotationX
        let x = toFixedFn( width/2 - Math.cos(angle) * width / 2)
        let z = toFixedFn( width/2 * Math.sin(angle) )
        targe.position.set(x, oldPosition.y, z + oz)
        targe.rotation.y = angle
    }).onComplete(()=>{
        console.log("动画结束")
        console.log(targe)
        targe.userData.openStatus = false
    }).start();
}

function toFixedFn(value:number) {
    return Number(value.toFixed(2))
}